local sharedLibs = game.ReplicatedStorage.SharedLibs;
local oo = require(sharedLibs.BaseLib)

AABB = require(script.Parent.AABB);
MathUtil = require(script.Parent.MathUtil);

local RandF = MathUtil.RandF;

---
local CylinderArea = oo.class("CylinderArea");

function CylinderArea:constructor(part)
	assert(part.Shape == Enum.PartType.Cylinder);
	self._part = part;
	self._reverseCF = part.CFrame:inverse();

	local size = self._part.Size;
	self._radius = math.min(size.Y, size.Z) / 2;
	self._radiusSquare = self._radius * self._radius; 
	self._halfHeight = size.X / 2;
end

function CylinderArea:GetRndPt()
	local x = RandF(-self._halfHeight, self._halfHeight);
	local y, z = MathUtil.OnUnitCircle2();
	local pt = Vector3.new(x, y * RandF(0, self._radius), z * RandF(0, self._radius));
	
	local pos = self._part.CFrame * pt; 
	return pos;
end

function CylinderArea:CheckPt(pt)
	pt = self._reverseCF * pt;	
	if pt.Y * pt.Y + pt.Z * pt.Z <= self._radiusSquare 	 
		and pt.X >= -self._halfHeight and pt.X <= self._halfHeight then
		return true;
	else
		return false;
	end
end

return CylinderArea;